package com.fy.oa.zc.menu.controller;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fy.oa.zc.menu.manager.FyReportMenuAccessManager;
import com.fy.oa.zc.menu.manager.FyReport_AuditTimeManager;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.authenticate.domain.User;
import com.seeyon.ctp.common.controller.BaseController;

public class FyReportMenuController extends BaseController {
	private static org.apache.commons.logging.Log log = LogFactory.getLog(FyReportMenuController.class);
	private FyReportMenuAccessManager fyReportMenuAccessManager;
	private FyReport_AuditTimeManager fyReport_AuditTimeManager;

	private static final String AUDIT_TIME = "1";
	private static final String AUDIT_TIME_SET = "2";
	private static final String FORM_QUERY = "3";
	private static final String FORM_QUERY_SET = "4";
	private static final String AUDIT_TIME_JSP = "fy_view/menu/report/auditTime";

	public void setFyReportMenuAccessManager(FyReportMenuAccessManager fyReportMenuAccessManager) {
		this.fyReportMenuAccessManager = fyReportMenuAccessManager;
	}

	public void setFyReport_AuditTimeManager(FyReport_AuditTimeManager fyReport_AuditTimeManager) {
		this.fyReport_AuditTimeManager = fyReport_AuditTimeManager;
	}

	/**
	 * 获取审批时长报表的JSP界面
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView getAuditTimeReportJSP(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		User user = AppContext.getCurrentUser();
		if (user != null && fyReportMenuAccessManager.haveAccess(String.valueOf(user.getId()), AUDIT_TIME)) {
			ModelAndView mav = new ModelAndView(AUDIT_TIME_JSP);
			return mav;
		}
		response.setCharacterEncoding("utf-8");
		PrintWriter writer = response.getWriter();
		writer.print("<html><body>您没有权限访问此界面！</body></html>");
		writer.flush();
		writer.close();
		return null;
	}

	/**
	 * 获取审批时长信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView getAuditTimeInfo(HttpServletRequest request, HttpServletResponse response) throws Exception {
		User user = AppContext.getCurrentUser();
		response.setCharacterEncoding("utf-8");
		PrintWriter writer = response.getWriter();
		if (user != null && fyReportMenuAccessManager.haveAccess(String.valueOf(user.getId()), AUDIT_TIME)) {
			long accountID = user.getLoginAccount();
			String fromdate = request.getParameter("fromdate");
			String todate = request.getParameter("todate");
			String year = null;
			if ("".equals(fromdate)) {
				year = new java.sql.Timestamp(java.util.Calendar.getInstance().getTimeInMillis()).toString()
						.substring(0, 4);
				fromdate = year + "-01-01 00:00:00";
			}
			if ("".equals(todate)) {
				todate = (year == null ? new java.sql.Timestamp(java.util.Calendar.getInstance().getTimeInMillis())
						.toString().substring(0, 4) : year) + "-12-31 23:59:59";
			}
			System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>");

			List<Map<String, Object>> result = null;
			try {
				System.out.println(fromdate);
				System.out.println(todate);
				System.out.println(accountID);

				result = fyReport_AuditTimeManager.queryDealTime(fromdate, todate, accountID);
			} catch (Exception e) {
				log.error("查询表单处理时长发生异常>>>>>>>>", e);
			}
			if (result == null) {
				writer.print("{success:false,msg:'没有查到任何信息'}");
			} else {
				System.out.println(result.size());
				ObjectMapper mapper = new ObjectMapper();
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("success", "true");
				map.put("result", result);
				writer.print(mapper.writeValueAsString(map));
			}
			System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<");
		} else {
			writer.print("{success:false,msg:'您没有权限做此操作'}");
		}
		writer.flush();
		writer.close();
		return null;
	}
}
